import common from '../../utils/common.js'
import resetData from '../../utils/resetData.js'
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    book_detail: null,
    mask1_show: false,//引导
    fold: true,//折叠
    is_action: false, //点击请求
    $toast: {
      show: false,
      text: ''
    },
    dialog_content: {

    },
    id:null
  },
  getbookdetails(id) { //isborrow：0未借阅 1的话正在借阅中 2.申请中 
    let self = this
    wx.showNavigationBarLoading()
    app.getToken(token => {
      app.getApi.getbookdetails({ id: id, token: token }).then(res => {
        wx.hideNavigationBarLoading();
        if (res.code == 3000) {
          if (res.data.guide == 0) { //首次借阅引导图，0：首次借阅1：非首次借阅
            self.data.mask1_show = true
          }
          if (res.data.shareuser.length){
            res.data.shareuser.forEach((item,index)=>{
              // let a
            })
          }
          self.setData({
            'book_detail': res.data,
            'mask1_show': self.data.mask1_show,
          }, () => {
            self.setData({
              loading: false
            })
          })
        } else {
          common.showToast(self, res.message)
        }
      }).catch(Error => {
        console.info(Error)
        wx.hideNavigationBarLoading();
      })
    })
  },
  collectionbook() {
    let self = this
    let book_detail = this.data.book_detail
    if (self.data.is_action) {
      return
    }
    self.setData({ is_action: true }, () => {
      app.getToken(token => {
        app.getApi.collectionbook({
          token: token,
          book_id: book_detail.id,
          operation: Number(!book_detail.collection)
        }).then(res => {
          let text
          if (res.code == 3000) {
            text = !book_detail.collection ? '已想读' : '取消想读'
            self.setData({
              [`book_detail.collection`]: !book_detail.collection,
              [`is_action`]: false
            })
            common.showToast(self, text)
          } else {
            common.showToast(self, res.message)
          }
        }).catch(Error => {
          console.info(Error)
        })
      })
    })
  },
  foldDesc() {
    this.setData({
      'fold': !this.data.fold
    })
  },
  showDialog(e) {
    let self = this
    let reader = e.currentTarget.dataset.reader
    if (reader.ifstatus == 2 || this.data.book_detail.isborrow != 0 || this.data.book_detail.isallow == 1 || reader.state == 4 || this.data.book_detail.isupload == 1) { // 0：不可见（暂不使用）1：未分享2：已分享(若已借出，则头像名字变灰)
      return common.showToast(self, '您目前不可向该用户借阅哦～')
    }
    let dialogComponent = this.selectComponent('.wxc-dialog')
    self.setData({
      dialog_content: reader
    }, () => {
      dialogComponent && dialogComponent.show();
    })
  },
  onConfirm() {
    let self = this
    let borrowbook = self.data.dialog_content
    let dialogComponent = this.selectComponent('.wxc-dialog');
    app.getToken(token => {
      app.getApi.borrowbook({
        token: token,
        book_id: self.data.book_detail.id,
        owner_id: borrowbook.id,
        owner_name: borrowbook.user_name
      }).then(res => {
        if (res.code == '3000') {
          dialogComponent && dialogComponent.hide();
          common.showToast(self, '申请借阅成功');
          self.setData({ [`book_detail.isborrow`]:2})
        }else{
          common.showToast(self, res.message)
        }
      }).catch(Error => {

      })
    })
  },
  onCancel() {
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.hide();
  },
  goRouter() {
    let self = this
    let book_detail = {
      name: self.data.book_detail.name,
      author: self.data.book_detail.author,
      publisher: self.data.book_detail.publisher,
      publish_time: self.data.book_detail.publish_time
    }
    wx.navigateTo({
      url: `../detailinfo/index?book_detail=${JSON.stringify(book_detail)}`,
    })
  },
  addSchool() {
    wx.navigateTo({
      url: '../myinfo/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    this.setData({
      id:id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getbookdetails(this.data.id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})